﻿Imports System.Data.Common

Public Class DLBaseBusiness
    Inherits DLBase

#Region "Declaration"

    Protected m_strTableDetailName As String
    Protected m_strPrimaryKeyTableDetailName As String
    Protected m_strForeignKeyName As String

#End Region

#Region "Property"

    Public Overridable Property TableDetailName() As String
        Get
            Return m_strTableDetailName
        End Get
        Set(ByVal value As String)
            m_strTableDetailName = value
        End Set
    End Property

    Public Property TableDetailPrimaryKey() As String
        Get
            Return m_strPrimaryKeyTableDetailName
        End Get
        Set(ByVal value As String)
            m_strPrimaryKeyTableDetailName = value
        End Set
    End Property

    Public Property TableDetailForeignKey() As String
        Get
            Return m_strForeignKeyName
        End Get
        Set(ByVal value As String)
            m_strForeignKeyName = value
        End Set
    End Property


#End Region

#Region "Constructor"

    Public Sub New(ByVal databaseConnectionName As String)
        MyBase.New(databaseConnectionName)
    End Sub

#End Region

#Region "Method"

    Public Overridable Function GetDetailDataByMasterID(ByVal strMasterID As Guid) As DataTable
        Dim ds As New DataSet
        Dim cmd As DbCommand = db.GetSqlStringCommand(Me.GetDetailScript())

        With cmd
            .Parameters.Add(strMasterID)
        End With

        db.LoadDataSet(cmd, ds, New String() {Me.TableDetailName})
        Return ds.Tables(0)
    End Function

    Public Overridable Sub GetDetailDataByMasterID(ByRef dtDetail As DataTable, ByVal strMasterID As Guid)

        Dim cmd As DbCommand = db.GetSqlStringCommand(Me.GetDetailScript())
        Dim para As DbParameter = cmd.CreateParameter
        With cmd
            para.ParameterName = mscParamToken & Me.TableDetailForeignKey
            para.DbType = DbType.Guid
            para.Direction = ParameterDirection.Input
            para.Value = strMasterID
            .Parameters.Add(para)
        End With

        db.LoadDataSet(cmd, dtDetail.DataSet, New String() {dtDetail.TableName})
    End Sub


#Region "Private Method"

    Protected Overridable Function GetDetailScript() As String
        Dim strSQL As String = "SELECT * FROM " & Me.TableDetailName & " WHERE " & Me.TableDetailForeignKey & " = " & _
                               mscParamToken & Me.TableDetailForeignKey _
                               & " Order BY SortOrder"
        Return strSQL
    End Function

#End Region

#Region "Public Method"

#End Region

#Region "Overridable"

#End Region

#End Region

#Region "Event"

#Region "Form event"

#End Region

#Region "Control event"

#End Region

#Region "User event"

#End Region

#End Region
End Class
